package com.springinaction.ch04.aopargs;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Aspect
public class TrackCounter {
    private Map<Integer,Integer> trackCounts =new HashMap<Integer, Integer>();
    @Pointcut("execution(* com.springinaction.ch04.aopargs.CompactDisc.playTrack(int)) && args(trackNumber)")
    public void trackplay(int trackNumber){}

    @Before("trackplay(trackNumber)")
    public void countTrack(int trackNumber){
        trackCounts.put(trackNumber,getPlayCount(trackNumber)+1);
    }
    public int getPlayCount(int trackNumber){
        return trackCounts.containsKey(trackNumber)? trackCounts.get(trackNumber):0;
    }
}
